package com.itheima.mybatis.test.dao.impl;

import com.itheima.mybatis.test.dao.UserMapper;
import com.itheima.mybatis.test.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

/**
 * @author ：sean
 * @date ：Created in 2020/6/30
 * @description ：接口实现类
 * @version: 1.0
 */
public class UserMapperImpl implements UserMapper {

  private SqlSessionFactory sqlSessionFactory;
  private String namespace = UserMapper.class.getName();

  public UserMapperImpl(SqlSessionFactory sqlSessionFactory){
    this.sqlSessionFactory = sqlSessionFactory;
  }

  @Override
  public User findByUsername(String username) {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    User user = sqlSession.selectOne(namespace+".findByUsername",username);
    sqlSession.close();
    return user;
  }

  @Override
  public List<User> findAllUsers() {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    List<User> objectList = sqlSession.selectList(namespace+".findAllUsers");
    sqlSession.close();
    return objectList;
  }
}
